package common.action;
/**
 * 所有action的父类，继承Actionsupport,提供request,response,application,session
 * @author jyl
 * 
 */
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware{
	private static final long serialVersionUID = -1513311332990213727L;
	
	protected HttpServletResponse response;

	protected HttpServletRequest request;
	
	protected HttpSession session;
	
	protected String url; 
	
	protected String rootPath;
	   
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
		this.rootPath = this.request.getSession().getServletContext()
				.getRealPath("/");
		try {
			this.request.setCharacterEncoding("utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		this.response.setCharacterEncoding("utf-8");
	}
	public final String getUrl() throws UnsupportedEncodingException {
		if (StringUtils.isBlank(url)) {
			StringBuffer contextUrl = request.getRequestURL();
			StringBuffer basePath = contextUrl.delete(
					contextUrl.length() - request.getRequestURI().length(),
					contextUrl.length()).append(request.getContextPath())
					.append("/");
			String referer = request.getHeader("Referer");
			url = referer.substring(basePath.length(), referer.length());
			url=URLEncoder.encode(url, "UTF-8");
		}
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
